package com.fy.controller;


import cn.hutool.core.util.RandomUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fy.entity.Result;
import com.fy.entity.User;
import com.fy.service.IUserService;
import com.fy.utils.JwtUtils;
import com.fy.utils.SmsUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 晓岩
 * @since 2022-06-07
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    IUserService userService;
    @Autowired
    RedisTemplate redisTemplate;
    @Autowired
    AuthenticationManager authenticationManager;
    @RequestMapping("getCode")
    public Result getCode(@RequestBody User user){
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("tel",user.getTel());
        User user1 = userService.getOne(queryWrapper);
        if(user1==null){
            throw new UsernameNotFoundException("用户"+user1+"不存在");
        }
        int Intcode = RandomUtil.randomInt(1000, 9000);
        String code =Intcode+"";
        System.out.println("验证码"+code);
        redisTemplate.opsForValue().set(user.getCodeKey(),code,10, TimeUnit.MINUTES);
        redisTemplate.opsForValue().set(user.getCodeKey()+"tel",user.getTel(),10,TimeUnit.MINUTES);
        try{
            SmsUtils.sendSms(user.getTel(),code);
        }catch (Exception e){
            return new Result(201,"","验证码失败");
        }
        return Result.OK();
    }
    @RequestMapping("mylogin")
    public Result mylogin(@RequestBody User user){
        if(!StringUtils.hasText(user.getCode())||!StringUtils.hasText(user.getCodeKey())){
            return new Result(202,"","验证信息不足");
        }
        String code = (String)redisTemplate.opsForValue().get(user.getCodeKey());
        String tel =(String) redisTemplate.opsForValue().get(user.getCodeKey() + "tel");
        if(!code.equals(user.getCode())){
            return new Result(203,"","验证码有误");
        }
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("tel",user.getTel());
        User user1 = userService.getOne(queryWrapper);
        if(user1==null){
            throw new UsernameNotFoundException("用户"+user1+"不存在");
        }
        try{
            UsernamePasswordAuthenticationToken uptoken = new UsernamePasswordAuthenticationToken(user1.getUsername(),user1.getPassword());
            Authentication authentication = authenticationManager.authenticate(uptoken);
            String token = JwtUtils.createToken(user1.getUsername(), 10, JSONUtil.toJsonStr(authentication));
            HashMap<String, Object> map = new HashMap<>();
            map.put("token",token);
            map.put("userInfo",authentication.getPrincipal());
            return new Result(200,"",map);
        }catch (Exception e){
            return new Result(204,"",e);
        }
    }
}
